package TUILayer;
import java.util.Scanner;
import ControlLayer.DVDCtr;
import ControlLayer.ControlCollector;
/**
 * User interface to the DVD controller
 * @author(kbh)
 * @version(2005.03.31)
 * (FEN): 2006.02.24: Comments translated into English
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering
 *                    and loose coupling
 * (KBH): 2008.10.24: dvdmenu changed to If                   
 */

public class DvdUI
{
    public static DVDCtr dvdCtr;//DVD UI must know the DVD controller 
    
    
    public DvdUI(ControlCollector collector)
    {
        dvdCtr = collector.getD();
    }

    /**
     * This method begins the creation of TUI
     */    
    public void start()
    {
        dvdMenu();
    }
    
    
    /**
     * This method creates a menu for the DVD TUI, it also collects input from the user and calls one of 5 functions connected with DVDs.
     */
    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            //show menu and return the user's choise
            int choice =   writeDVDMenu();
            if (choice == 1)
            {
                findDVD();
            }
            else{if(choice == 2){
                   createDVD();
                }
                else{
                    if(choice == 3){
                        createCopy();
                    }
                    else{
                        if(choice == 4){
                             deleteDVD();
                        }
                        else{
                            if(choice == 5){
                                listAllDvds();
                            }
                            else{
                                exit = true;
                            }//end else
                        }//end else
                    }//end else
                }//end else
            }//end else
            
        }//end while
    }
    
    /**
     * This method prints a menu for a method dvdMenu.
     */    
    private int writeDVDMenu()
    {
           // creates an object keyboard to read data from the keyboard
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** DVD Menu ***");
            System.out.println(" (1) Show DVD information");
            System.out.println(" (2) Create DVD description");
            System.out.println(" (3) Create DVD specimen");
            System.out.println(" (4) Delete DVD");
            System.out.println(" (5) Show current DVD list");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choice = keyboard.nextInt();
            return choice;
    }
    
    /**
     * This method collects input, which is an id of the DVD, from the user.
     * Since the id has to be a number, this function will loop untill user provides it with correct input.
     */    
    private int inputDVDNo()
    {
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write DVD ID:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
          
    /**
     * This method collects input, which is atitle of a DVD, from the user.
     */  
    private String inputTitle()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type DVD title: " );
        String title = keyboard.nextLine();
        return title;
    }
        
    /**
     * This method collects input, which is a name of artist who created the DVD, from the user.
     */
    private String inputArtist()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type artist: ");
        String artist = keyboard.nextLine();
        return artist;
    }
       
    /**
     * This method collects input, which is a publication date of the DVD, from the user.
     */ 
    private String inputPublicationDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type publication date: ");
        String publicationDate = keyboard.nextLine();
        return publicationDate;
    }
    
    /**
     * This method collects input, which is an id of the Copy, from the user.
     * Since the id has to be a number, this function will loop untill user provides it with correct input.
     */    
    private int inputCopyNo()
    {   
        boolean ok = false;
        int id = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println(" Write copy ID:  ");
           try{
             id = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return id;
    }
       
    /**
     * This method collects input, which is a purchase date of the copy of DVD, from the user.
     */ 
    private String inputPurchaseDate()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the purchase date");
        String purchaseDate = keyboard.nextLine();
        return purchaseDate;
    }
       
    /**
     * This method collects input, which is a serial number of the copy of the DVD, from the user.
     */ 
    private String inputSerialNumber()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type the serial number");
        String serialNumber = keyboard.nextLine();
        return serialNumber;
    }
    
     /**
     * This method collects input, which is a purchase price of a Copy, from the user.
     * Since the price has to be a number, this function will loop untill user provides it with correct input.
     */  
    private double inputPurchasePrice()
    {
        boolean ok = false;
        double price = 0;
        while(!ok)
        {
        // makes an object keyboard to have input from the console
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Type the purchase price");
           try{
             price = keyboard.nextDouble();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("You must enter a number -  try again");
             String input = keyboard.nextLine();
           }
        }//end while
        return price;
    }
    
    /**
     * This method prints the i\data of a desired DVD.
     */
    private void findDVD()
    {     
        int no = inputDVDNo();
        dvdCtr.findDVD(no);
        waitForInput();
    }
    
    /**
     * This method collects DVD ID, title, artist name, and publication date from the user and then creates a new DVD using this data.
     */
    private void createDVD()
    {
        int id = inputDVDNo();
        if(dvdCtr.isThere(id))
        {
            System.out.println("The Id is already used");
        }
        else
        {
            String title = inputTitle();
            String artist = inputArtist();
            String publicationDate = inputPublicationDate();
            dvdCtr.createDVD(id, title, artist, publicationDate);
        }
        waitForInput();
            
    }
    
    /**
     * This method collects id of a copy of a DVD, DVD ID, serial number, purchase date, and purchase price from the user and then creates a new copy of a DVD using this data.
     */
    private void createCopy()
    {
        int dvdId = inputDVDNo();
        if(dvdCtr.isThere(dvdId))
        {
            int copyId = inputCopyNo();
            if(dvdCtr.hasCopy(dvdId, copyId))
            {
                System.out.println("There already is a copy of this DVD with this particular ID, choose a different one");
            }
            else
            {
                String serialNumber = inputSerialNumber();
                String purchaseDate = inputPurchaseDate();
                double purchasePrice = inputPurchasePrice();
                dvdCtr.createCopy(dvdId, copyId, serialNumber, purchaseDate, purchasePrice);
            }
        }
        else
        {
            System.out.println("There is no such DVD in the collection");
        }
        waitForInput();
    }
    
    /**
     * This method deletes desired DVD
     */
    private void deleteDVD()
    {
        int id = inputDVDNo();
        if(dvdCtr.isThere(id))
        {
            dvdCtr.deleteDVD(id);
        }
        waitForInput();
    }
    
    /**
     * This method prints data of all DVDs
     */
    private void listAllDvds()
    {
        dvdCtr.listAllDvds();
        waitForInput();
    }
   
    /**
     * This method makes the TUI wait for ENTER so it is possibble to read all the data on the screen.
     */
    private void waitForInput()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\nPress ENTER to continue\n");
        keyboard.nextLine();
    }
    
    
    

}